package com.web.wechat.global.controller;

import com.blue.core.dict.HttpMethod;
import com.blue.core.util.RandomUtil;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.validation.ValidationUtil;
import com.web.base.auth.exception.AppException;
import com.web.base.auth.facade.AppFacade;
import com.web.base.auth.message.AppResponse;
import com.web.base.log.facade.SmsFacade;
import com.web.base.user.facade.UserFacade;
import com.web.base.user.message.UserRequest;
import com.web.base.user.message.UserResponse;
import com.web.base.valid.UserLogin;
import com.web.base.valid.UserReg;
import com.web.base.wechat.facade.WechatFacade;
import com.web.base.wechat.message.WechatResponse;
import com.web.core.cache.ConfigCache;
import com.web.core.util.Const;
import com.web.wechat.global.model.User;
import com.web.wechat.global.model.UserWechat;
import com.web.wechat.global.util.HttpContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.io.IOException;

@Controller
@Http(url = "/app")
public class AppController
{
	private static Logger logger = LoggerFactory.getLogger(AppController.class);
	
	@Autowired
	private UserFacade userFacade;
	@Autowired
	private WechatFacade wechatFacade;
	@Autowired
	private AppFacade appFacade;
	@Autowired
	private SmsFacade smsFacade;
	@Resource
	private ConfigCache configCache;
	@Resource
	private HttpContext httpContext;
	
	public AppController()
	{
	}

	@Http(url = "/login", method = HttpMethod.GET)
	public View loginGet(HttpRequest request)
	{
		return View.createBuilder().put(Const.FTL_TITLE, "登录").setView("/app/login").build();
	}

	@Http(url = "/login", method = HttpMethod.POST)
	public void loginPost(HttpRequest request)
	{
		UserRequest userRequest = request.getPostObject(UserRequest.class);
		userRequest.setAppId(this.getAppId());
		this.check(request, userRequest);
		ValidationUtil.valid(request, UserLogin.class);
		UserResponse response = userFacade.login(userRequest);
		httpContext.setUser(request, User.toUser(response));
		logger.info("登录成功：{}", response.getNickname());
	}
	private void check(HttpRequest request, UserRequest userRequest)
	{
		UserWechat uw = httpContext.getUserWechat(request);
		if (uw != null)
		{
			userRequest.setWxOpenId(uw.getWxOpenId());
			userRequest.setWxUnionId(uw.getWxUnionId());
		}
		String code = configCache.getValue(Const.WECHAT_CODE);
		WechatResponse response = wechatFacade.getByCode(code);
		if (response != null)
		{
			userRequest.setWechatId(response.getId());
		}
		userRequest.setIp(request.getIp());

	}

	@Http(url = "/logout", method = HttpMethod.GET)
	public View logout(HttpRequest request) throws IOException
	{
		httpContext.setUser(request,null);
		String url = configCache.getValue(Const.WECHAT_PATH);
		System.out.println(url);
		return View.createBuilder().setLocation(url + "/app/login").build();
	}

	@Http(url = "/reg", method = HttpMethod.GET)
	public View regGet(HttpRequest request)
	{
		return View.createBuilder().put(Const.FTL_TITLE, "注册").setView("/app/reg").build();
	}

	@Http(url = "/reg", method = HttpMethod.POST)
	public void regPost(HttpRequest request)
	{
		UserRequest userRequest = request.getPostObject(UserRequest.class);
		userRequest.setAppId(this.getAppId());
		this.check(request, userRequest);
		ValidationUtil.valid(request, UserReg.class);
		UserResponse response = userFacade.reg(userRequest);
		httpContext.setUser(request, User.toUser(response));
		logger.info("注册成功：{}", response.getNickname());
	}

	@Http(url = "/sms", method = HttpMethod.POST)
	public void sms(HttpRequest request)
	{
		String mobile = request.getPost("mobile");
		String num = RandomUtil.rand(RandomUtil.RandomType.NUMBER, 6);
		smsFacade.sendMobileValid(mobile, num);
	}
	
	private Integer getAppId()
	{
		String code = configCache.getValue(Const.APP_WECHAT);
		AppResponse app = appFacade.getByCode(code);
		if (app == null)
			throw new AppException("应用不存在，code=" + code);
		
		return app.getId();
	}
	
}
